<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 俱乐部信息管理</el-breadcrumb-item>
                <el-breadcrumb-item>待审核用户列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10"
                :disabled = "flag"
                @click="allPassVisible = true">批量通过</el-button>
                <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </div>
            <el-table :data="data" border class="table" ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" width="55" align="center" label="序列"></el-table-column>
                <el-table-column label="头像" width="120">
                    <template slot-scope="scope">
                        <el-image style="width: 100px; height: 100px" fit="cover"
                        :src="`http://localhost/fitness/public/static/img/`+scope.row.useravatar"
                        @click="handlePictureCardPreview(scope.row.useravatar)">
                            <div slot="error" class="image-slot">
                                <i class="el-icon-picture-outline"></i>
                            </div>
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="realname" label="姓名" width="180">
                </el-table-column>
                <el-table-column prop="username" label="用户名" width="auto">
                </el-table-column>
                <el-table-column prop="userphone" label="电话" width="200">
                </el-table-column>
                <el-table-column prop="userapplyt" label="申请日期" sortable width="200">
                </el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-lx-roundcheck" @click="handlePass(scope.row)">通过</el-button>
                         <el-button type="text" icon="el-icon-delete" class="red" @click="handleReject(scope.row)">不通过</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 通过提示框 -->
        <el-dialog title="提示" :visible.sync="passVisible" width="300px" center>
            <div class="del-dialog-cnt">确认通过？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="passVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmPass">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 拒绝提示框 -->
        <el-dialog title="提示" :visible.sync="rejectVisible" width="300px" center>
            <div class="del-dialog-cnt">确认拒绝？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="rejectVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmReject">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 全部通过提示框 -->
        <el-dialog title="提示" :visible.sync="allPassVisible" width="300px" center>
            <div class="del-dialog-cnt">确认全部通过？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="allPassVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmAllPass">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 预览图片弹出框 -->
        <el-dialog :visible.sync="dialogImgVisible" width="25%">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
    </div>
</template>

<script>
    import { fetchData } from '../api/index';
    import { test } from '../api/index';
    export default {
        name: 'basetable',
        data() {
            return {
                waitforInData: [],
                multipleSelection: [],
                select_word: '',
                dialogImageUrl: '',
                del_list: [],
                is_search: false,
                passVisible: false,
                dialogImgVisible: false,
                rejectVisible: false,
                allPassVisible: false,
                flag: true,
                passid: 0,
                rejectid: 0
            }
        },
        created() {
            this.getData();
        },
        methods: {
            // TODO: 老规矩，读取session中的clubid
            getData() {
                this.$axios.get('/api/Club/waitforInList',{
                    params: {
                        clubid: localStorage.clubid
                    }
                }).then(res =>{
                    this.waitforInData = res.data;
                    // console.log('res.data :', res.data);
                })
            },
            search() {
                
            },
            handlePass(row) {
                this.passVisible = true;
                this.passid = row.userid;
            },
            handleReject(row) {
                this.rejectVisible = true;
                this.rejectid = row.userid;
            },
            confirmAllPass() {
                const length = this.multipleSelection.length;
                for (let i = 0; i < length; i++) {
                    this.$axios.get('/api/Club/clubOnUser',{
                        params: {
                            userid: this.multipleSelection[i].userid
                        }
                    });
                }
                setTimeout(() => {
                    this.getData();
                    this.allPassVisible = false;
                    this.$message.success('通过了 ' + length + ' 个用户');
                }, 500);
                this.multipleSelection = [];                   
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
                if(this.multipleSelection.length===0){
                    this.flag = true;
                }else{
                    this.flag = false;
                }
            },
            handlePictureCardPreview(url) {
                this.dialogImageUrl =`http://localhost/fitness/public/static/img/`+url;
                console.log('object :', this.dialogImageUrl);
                this.dialogImgVisible = true;
            },
            confirmPass(){
                this.$axios.get('/api/Club/clubOnUser',{
                    params: {
                        userid: this.passid
                    }
                }).then(res => {
                    this.getData();
                    this.passVisible = false;
                })
            },
            confirmReject(){
                this.$axios.get('/api/Club/rejectUser',{
                    params: {
                        'userid': this.rejectid
                    }
                }).then(res => {
                    this.getData();
                    this.rejectVisible = false;
                })
            }
        },
        computed: {
            data() {
                return this.waitforInData.filter(d => {
                    if(d.realname){
                        if(d.realname.indexOf(this.select_word) > -1 || d.username.indexOf(this.select_word) > -1){
                            return d;
                        }
                    }else{
                        if(d.username.indexOf(this.select_word) > -1){
                            return d;
                        }
                    }
                })
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    .mr10{
        margin-right: 10px;
    }
</style>
